package com.xqls.biz.impl;

import java.util.*;

import com.xqls.biz.FluggasBlockAdministrationBiz;
import com.xqls.biz.FluggasTableFieldBiz;
import com.xqls.redis.Constants;
import com.xqls.redis.RedisCache;
import com.xqls.redis.RedisKeyEnum;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xqls.bean.FluggasDischargeRealdataBean;
import com.xqls.biz.FluggasDischargeRealdataBiz;
import com.xqls.dal.model.FluggasDischargeRealdata;
import com.xqls.service.FluggasDischargeRealdataService;
import com.xqls.util.CommonPage;
import com.xqls.util.CommonResult;
import com.xqls.util.ObjectUtil;
import com.xqls.util.VelocityUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Service
public class FluggasDischargeRealdataBizImpl implements FluggasDischargeRealdataBiz {

	private static Logger logger = Logger.getLogger(FluggasDischargeRealdataBiz.class);

	@Autowired
	private FluggasDischargeRealdataService realdataService;
	@Autowired
	private RedisCache<String, Object> redisCache;

	@Autowired
	private FluggasBlockAdministrationBiz FluggasBlockAdministrationBiz;

	@Autowired
	private FluggasTableFieldBiz FluggasTableFieldBiz;

	@Override
	public CommonResult insertRealdata(FluggasDischargeRealdataBean recordBean) {
		CommonResult result = new CommonResult();
		FluggasDischargeRealdata record = null;
		try {
			if (!this.realdataService.updateRealdata(recordBean)) {
				this.realdataService.insertRealdata(recordBean);
			}
			result.setIsSuccess(true);
			result.setResultMsg("实时数据保存成功！");
		} catch (Exception e) {
			logger.error("实时数据保存异常！", e);
		}
		return result;
	}

	@Override
	public CommonResult getListByPage(CommonPage commonPage, HttpServletRequest request, HttpServletResponse response) {
		CommonResult result = new CommonResult();

		try {
			List<String> columns_name = new ArrayList<String>();
			List<Object> columns = new ArrayList<Object>();
			List<String> displayfiled = FluggasTableFieldBiz.FieldEnglish(FluggasBlockAdministrationBiz.BlockAdministration(commonPage.pageToMap()));
			columns.add("viewId");
			for (String display : displayfiled) {
				if(!display.equals("realdata_no")){
					columns.add(display);
				}

			}

			List<String> fieldalias =  FluggasTableFieldBiz.FieldChinese(FluggasBlockAdministrationBiz.BlockAdministration(commonPage.pageToMap()));
			columns_name.add("序号");
			for (String fieldalia : fieldalias) {
				if(!fieldalia.equals("编号")){
					columns_name.add(fieldalia);
				}

			}

			List<Map<String, Object>> datas = realdataService.selectByMap(commonPage.pageToMap());
			List<String> allstate= new ArrayList<>();
			for(int i=0;i<datas.size();i++){
				String state="";
				Iterator it=datas.get(i).keySet().iterator();
				while(it.hasNext()){
					String key ="";
					String value="";
					key = it.next().toString();
					for (String display : displayfiled) {
						if (key.indexOf(display+"_state") >= 0) {
							String start="";
							if(("O2_content_state").equals(key)){
								start+="O2含量设备";
							}else if(("smoke_velocity_state").equals(key)){
								start+="烟气流速设备";
							}else if(("smoke_temperature_state").equals(key)){
								start+="烟气温度设备";
							}else if(("smoke_dynamic_pressure_state").equals(key)){
								start+="烟气动压设备";
							}else if(("smoke_humidity_state").equals(key)){
								start+="烟气湿度设备";
							}else if(("smoke_pressure_state").equals(key)){
								start+="烟气压力设备";
							}else if(("smoke_state").equals(key)){
								start+="烟尘设备";
							}else if(("SO2_state").equals(key)){
								start+="二氧化硫设备";
							}else if(("NO2_state").equals(key)){
								start+="氮氧化物设备";
							}else if(("CO_state").equals(key)){
								start+="一氧化碳设备";
							}else if(("H2S_state").equals(key)){
								start+="硫化氢设备";
							}else if(("HCl_state").equals(key)){
								start+="氯化氢设备";
							}else if(("NH3_state").equals(key)){
								start+="氨设备";
							}else if(("Cl2_state").equals(key)){
								start+="氯气设备";
							}else if(("CS2_state").equals(key)){
								start+="二硫化碳设备";
							}else if(("benzene_series_state").equals(key)){
								start+="苯系物设备";
							}else if(("toluene_state").equals(key)){
								start+="甲苯设备";
							}else if(("xylene_state").equals(key)){
								start+="二甲苯设备";
							}else if(("formaldehyde_state").equals(key)){
								start+="甲醛设备";
							}else if(("Phenols_state").equals(key)){
								start+="酚类设备";
							}else if(("methanol_state").equals(key)){
								start+="甲醇设备";
							}else if(("vinyl_chloride_state").equals(key)){
								start+="氯乙烯设备";
							}else if(("CO2_state").equals(key)){
								start+="二氧化碳设备";
							}

							value = datas.get(i).get(key).toString();
							if(("故障").equals(value)) {
								state += start + ":" + value + ";";
							}
						}
					}

				}
				allstate.add(state);
			}
			int curr=commonPage.getCurr();
			for (int i = 0;i<datas.size();i++){
				datas.get(i).put("viewId", i + 1+((curr-1)*15));
				datas.get(i).put("all_state",allstate.get(i));
			}

			commonPage.setPageData(datas);

			Map<String, Object> showMap = new HashMap<String, Object>();
			showMap.put("realdataList", commonPage.getPageData());
			showMap.put("userPerms", redisCache.get(Constants.getRedisKey(RedisKeyEnum.USER_PERMS, SecurityUtils.getSubject().getPrincipal().toString())));
			showMap.put("allstate",allstate);
			showMap.put("columns", CommonPage.setColumnToPage(columns_name.toArray(new String[columns_name.size()]),
					columns.toArray(), null, "废气排放口实时数据"));
			commonPage.setHtml(VelocityUtil.getHtmlByMap("Realdata/realdataListData.vm", showMap,request,response,request.getSession().getServletContext()));
			result.setIsSuccess(true);

		} catch (Exception e) {
			logger.error("查询废气排放口实时数据异常！", e);
		}
		return result;
	}

	@Override
	public FluggasDischargeRealdataBean getByDeviceNo(String deviceNo) {
		FluggasDischargeRealdataBean bean=null;
		List<FluggasDischargeRealdata> list=null;
		try {
          list = this.realdataService.getListByDeviceNo(deviceNo);
          if(!list.isEmpty())
          {
        	 bean=(FluggasDischargeRealdataBean) ObjectUtil.transfer(list.get(0), FluggasDischargeRealdataBean.class);
          }
		} catch (Exception e) {
			logger.error("查询废气排放口实时数据异常！", e);
		}
		return bean;
	}

	@Override
	public List<Map<String, Object>> getAll() {
		List<Map<String, Object>> list=null;
		try {
            list=this.realdataService.getListAll();
		} catch (Exception e) {
			logger.error("查询废气排放口实时数据异常！", e);
		}
		return list;
	}

}
